package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.dto.DtItemDTO;
import com.dk.dto.EsDtItemDTO;
import com.dk.em.ResultEnum;
import com.dk.service.EsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("esController")
@Api(value = "全文搜索")
public class EsController {
    @Resource
    private EsService esService;

    @PostMapping("all")
    @ApiIgnore(value = "全文搜索接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNumber",value = "当前页",required = true,paramType = "query"),
            @ApiImplicitParam(name = "word",value = "输入搜索关键词",required = true,paramType = "query"),
    })
    public Result getWord(@RequestParam(defaultValue = "1")Integer pageNumber,
                          @RequestParam(defaultValue = "3")Integer pageSize,
                          @RequestParam("word")String word){
        Map<String,Object> map = esService.getWord(word,pageNumber,pageSize);
        EsDtItemDTO DtItemDTO = JSONObject.parseObject(JSONObject.toJSONString(map), EsDtItemDTO.class);
        return Result.response(ResultEnum.SUCCESS).setData(DtItemDTO);
    }
    @PostMapping("nearby")
    @ApiIgnore(value = "附近搜索接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNumber",value = "当前页",required = true,paramType = "query"),
            @ApiImplicitParam(name = "word",value = "输入搜索关键词",required = true,paramType = "query"),
    })
    public Result getNearbyByWord(@RequestParam(defaultValue = "1")Integer pageNumber,
                          @RequestParam(defaultValue = "3")Integer pageSize,
                          @RequestParam("word")String word){
        Map<String,Object> map = esService.getNearbyByWord(word,pageNumber,pageSize);
        DtItemDTO dtItemDTO = JSONObject.parseObject(JSONObject.toJSONString(map), DtItemDTO.class);
        return Result.response(ResultEnum.SUCCESS).setData(dtItemDTO);
    }
}
